package manage.actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import manage.commons.Constants;
import manage.commons.utils.ServletUtils;

import org.apache.commons.lang3.math.NumberUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 对一些基本共用属性，方法的封装
 * 
 * @author dongji
 * 
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {

	private static final long serialVersionUID = -1369527032117369800L;
	private static final String ENCODING = Constants.DEFAULT_ENCODE;
	protected HttpServletRequest request;
	protected HttpServletResponse response;

	/**
	 * 获得HttpServletRequest对象
	 * 
	 * @return HttpServletRequest
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获得HttpServletResponse对象
	 * 
	 * @return HttpServletResponse
	 */
	protected HttpServletResponse getResponse() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=" + ENCODING);
		response.setCharacterEncoding(ENCODING);
		return response;
	}

	/**
	 * 获得HttpSession对象,不存在则重新创建session
	 * 
	 * @return HttpSession
	 */
	protected HttpSession getSession() {
		return this.getRequest().getSession(true);
	}

	/**
	 * 获得HttpSession对象
	 * 
	 * @param flag
	 *            如果当前session不存在是否重新创建session
	 * @return HttpSession
	 */
	protected HttpSession getSession(boolean flag) {
		return this.getRequest().getSession(flag);
	}

	/**
	 * 获得ServletContext对象
	 * 
	 * @return ServletContext
	 */
	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	/**
	 * 获得path所在真实路径
	 * 
	 * @return path所在真实路径
	 */
	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}

	/**
	 * 获得response输出对象
	 * 
	 * @return PrintWriter
	 * @throws IOException
	 */
	protected PrintWriter getOut() throws IOException {
		HttpServletResponse response = this.getResponse();
		return response.getWriter();
	}

	/**
	 * 使用response输出流输出信息
	 * 
	 * @return PrintWriter
	 * @throws IOException
	 */
	protected void print(String info) throws IOException {
		HttpServletResponse response = this.getResponse();
		response.getWriter().print(info);
	}

	protected Enumeration<?> getParameterNames() {
		return this.getRequest().getParameterNames();
	}

	/**
	 * 获取参数值（字符串）
	 * 
	 * @param parameterName
	 *            要获取参数的名称
	 * @return 参数的值
	 */
	protected String getParameterForString(String parameterName) {
		return this.getRequest().getParameter(parameterName);
	}

	/**
	 * 获取参数值（长整型）
	 * 
	 * @param parameterName
	 *            要获取参数的名称
	 * @return 参数的值
	 */
	protected long getParameterForLong(String parameterName)
			throws NumberFormatException {
		if (this.getRequest().getParameter(parameterName) == null
				|| this.getRequest().getParameter(parameterName) == "") {
			return -1;
		}
		return NumberUtils
				.toLong(this.getRequest().getParameter(parameterName));
	}

	/**
	 * 获取参数值（整型）
	 * 
	 * @param parameterName
	 *            要获取参数的名称
	 * @return 参数的值
	 */
	protected int getParameterForInt(String parameterName)
			throws NumberFormatException {
		if (this.getRequest().getParameter(parameterName) == null
				|| this.getRequest().getParameter(parameterName) == "") {
			return -1;
		}
		return NumberUtils.toInt(this.getRequest().getParameter(parameterName));
	}

	/**
	 * 获取参数值(字符串数组)
	 * 
	 * @param parameterName
	 *            要获取参数的名称
	 * @return 参数的值
	 */
	protected String[] getParameterValues(String parameterName)
			throws Exception {
		return this.getRequest().getParameterValues(parameterName);
	}

	protected Map<?, ?> getParameterMap() throws Exception {
		return this.getRequest().getParameterMap();
	}

	/**
	 * 往request里面设置值
	 * 
	 * @param key
	 *            要设置的关键字
	 * @param obj
	 *            要设置的值
	 */
	protected void setAttribute(String key, Object obj) {
		this.getRequest().setAttribute(key, obj);
	}

	/**
	 * 获得session的值
	 * 
	 * @param <T>
	 *            泛型
	 * @param attrName
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected <T> T getSessionAttr(String attrName, Class<T> clazz) {
		return (T) this.getSession(false).getAttribute(attrName);
	}

	/**
	 * 往session里面设置值
	 * 
	 * @param attrName
	 *            关键字
	 * @param obj
	 *            设置的值
	 */
	protected void setSessionAttr(String attrName, Object obj) {
		this.getSession(false).setAttribute(attrName, obj);
	}

	/**
	 * 取得ip
	 * 
	 * @return
	 */
	protected final String getRemoteIP() {
		if (request == null)
			return "";
		return ServletUtils.getRealIp(request);
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
